<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <body>
    <?php
    $scriptName = "RegisterUser.php";
    include("PHPprinter.php");
    $startTime = getMicroTime();
    
    $firstname = $HTTP_POST_VARS['firstname'];
    if ($firstname == null)
    {
      $firstname = $HTTP_GET_VARS['firstname'];
      if ($firstname == null)
      {
         printError($scriptName, $startTime, "Register user", "You must provide a first name!<br>");
         exit();
      }
    }
      
    $lastname = $HTTP_POST_VARS['lastname'];
    if ($lastname == null)
    {
      $lastname = $HTTP_GET_VARS['lastname'];
      if ($lastname == null)
      {
         printError($scriptName, $startTime, "Register user", "You must provide a last name!<br>");
         exit();
      }
    }
      
    $nickname = $HTTP_POST_VARS['nickname'];
    if ($nickname == null)
    {
      $nickname = $HTTP_GET_VARS['nickname'];
      if ($nickname == null)
      {
         printError($scriptName, $startTime, "Register user", "You must provide a nick name!<br>");
         exit();
      }
    }

    $email = $HTTP_POST_VARS['email'];
    if ($email == null)
    {
      $email = $HTTP_GET_VARS['email'];
      if ($email == null)
      {
         printError($scriptName, $startTime, "Register user", "You must provide an email address!<br>");
         exit();
      }
    }

    $password = $HTTP_POST_VARS['password'];
    if ($password == null)
    {
      $password = $HTTP_GET_VARS['password'];
      if ($password == null)
      {
         printError($scriptName, $startTime, "Register user", "You must provide a password!<br>");
         exit();
      }
    }

    getDatabaseLink($link);

    // Check if the nick name already exists
    $nicknameResult = mysql_query("SELECT * FROM users WHERE nickname=\"$nickname\"", $link) or die("ERROR: Nickname query failed");
    if (mysql_num_rows($nicknameResult) > 0)
    {
      printError($scriptName, $startTime, "Register user", "The nickname you have choosen is already taken by someone else. Please choose a new nickname.<br>\n");
      mysql_free_result($nicknameResult);
      exit();
    }
    mysql_free_result($nicknameResult);

    // Add user to database
    $now = date("Y:m:d H:i:s");
    $result = mysql_query("INSERT INTO users VALUES (NULL, \"$firstname\", \"$lastname\", \"$nickname\", \"$password\", \"$email\", 0, 0, '$now')", $link) or die("ERROR: Failed to insert new user in database.");

    $result = mysql_query("SELECT * FROM users WHERE nickname=\"$nickname\"", $link) or die("ERROR: Query user failed");
    $row = mysql_fetch_array($result);

    printHTMLheader("RUBBoS: Welcome to $nickname");
    print("<h2>Your registration has been processed successfully</h2><br>\n");
    print("<h3>Welcome $nickname</h3>\n");
    print("RUBBoS has stored the following information about you:<br>\n");
    print("First Name : ".$row["firstname"]."<br>\n");
    print("Last Name  : ".$row["lastname"]."<br>\n");
    print("Nick Name  : ".$row["nickname"]."<br>\n");
    print("Email      : ".$row["email"]."<br>\n");
    print("Password   : ".$row["password"]."<br>\n");
    print("<br>The following information has been automatically generated by RUBBoS:<br>\n");
    print("User id       :".$row["id"]."<br>\n");
    print("Creation date :".$row["creation_date"]."<br>\n");
    print("Rating        :".$row["rating"]."<br>\n");
    print("Access        :".$row["access"]."<br>\n");
    
    mysql_free_result($result);
    mysql_close($link);
    
    printHTMLfooter($scriptName, $startTime);
    ?>
  </body>
</html>
